<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
    <el-form-item label="演练名称" label-width='80px'>
        <el-input
          v-model="queryParams.drillName"
          placeholder="请输入演练名称"
          clearable
           @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="演练时间" prop="documentName">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd"
          default-time="00:00:00"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="结束时间" prop="documentName">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd"
          default-time="00:00:00"
        >
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="创建人id" prop="creatId">
          <el-input
            v-model="queryParams.creatId"
            placeholder="请输入创建人id"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:emergencyDrill:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:emergencyDrill:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:emergencyDrill:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['app:emergencyDrill:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="documentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column label="演练名称" align="center" prop="drillName" />
      <el-table-column label="演练单位" align="center" prop="drillUnit" />
      <el-table-column label="演练地点" align="center" prop="drillPlace" />
      <el-table-column label="演练时间" align="center" prop="drillTime" >
        <!-- <template slot-scope="scope">
              <span>{{ parseTime(scope.row.drillTime, "{y}-{m}-{d} {h}:{m}:{s}") }}</span>
            </template> -->
      </el-table-column>
      <el-table-column label="组织部门" align="center" prop="organizationdep" />
      <el-table-column label="参与部门" align="center" prop="actor" />
      <el-table-column label="演练内容" align="center" prop="drillContent">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.drillContent != ''"
            type="text"
            @click="showContent(scope.row.drillContent)"
            >查看</el-button
          >
        </template>
      </el-table-column>

      <el-table-column label="演练类型" align="center" prop="drillWay">
        <template slot-scope="scope">
          {{ scope.row.drillWay == 1 ? "桌面演练" : "实战演练" }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="nickName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="文件" align="center" prop="filePaths">
        <template slot-scope="scope">
          <div v-if="scope.row.filePaths">
            <el-button
              v-if="gettp(scope.row.filePaths) == 1"
              size="mini"
              type="text"
              @click="look(scope.row.filePaths)"
              >查看</el-button
            >
            <el-button
              v-if="gettp(scope.row.filePaths) == 2"
              size="mini"
              type="text"
              @click="download(scope.row.filePaths)"
              >下载</el-button
            >
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="矿山id" align="center" prop="mineId" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:emergencyDrill:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:emergencyDrill:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外包单位资质管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="演练名称" prop="drillName">
          <el-input v-model="form.drillName" placeholder="请输入演练名称" />
        </el-form-item>
        <el-form-item label="演练单位" prop="drillName">
          <el-input v-model="form.drillUnit" placeholder="请输入演练单位" />
        </el-form-item>
        <el-form-item label="演练地点" prop="drillPlace">
          <el-input v-model="form.drillPlace" placeholder="请输入演练地点" />
        </el-form-item>
        <el-form-item label="演练时间" prop="drillTime">
          <!-- <el-input v-model="form.drillTime" placeholder="请输入文件名称" /> -->
          <el-date-picker
            clearable
            v-model="form.drillTime"
            type="date"
            placeholder="选择演练时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="演练类型" prop="drillWay">
          <el-select v-model="form.drillWay" placeholder="请选演练类型">
            <el-option label="桌面演练" :value="1"></el-option>
            <el-option label="实战演练" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="组织部门" prop="organizationdep">
          <el-input
            v-model="form.organizationdep"
            placeholder="请输入组织部门"
          />
        </el-form-item>
        <el-form-item label="参与部门" prop="actor">
          <el-input v-model="form.actor" placeholder="请输入参与部门" />
        </el-form-item>
        <el-form-item label="演练内容" prop="drillContent">
          <el-input v-model="form.drillContent" placeholder="请输入演练内容" />
        </el-form-item>
        <el-form-item label="文件" prop="filePaths">
          <FileUpload
            v-model="form.filePaths"
            @input="upImage"
            :limit="5"
          ></FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="contenShow"
      width="500px"
      append-to-body
    >
      <p>{{ contentList }}</p>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="contenShow = false">确 定</el-button>
      </div>
    </el-dialog>
    <preview-content v-if="showType" :previewMsg="previewMsg"></preview-content>
  </div>
</template>
    
    <script>
import {
  listDocument,
  getDocument,
  delDocument,
  addDocument,
  updateDocument,
  addemergencyDrill,
  listemergencyDrill,
  deleteemergencyDrill,
} from "@/api/app/yearPlan";
import { downloadFile } from "@/utils/download.js";
import { listUnit } from "@/api/system/outunit";
import cache from "@/plugins/cache";
import PreviewContent from "@/components/PreviewContent/PreviewContent.vue";
export default {
  components: {
    PreviewContent,
  },
  name: "Document",
  data() {
    return {
      contenShow: false,
      contentList: "",
      showType: true,
      userId: cache.session.getJSON("userId"),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外包单位资质管理表格数据
      documentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
        drillTime:null,
        drillName:null
      },
      previewMsg: {
        dialogTableVisible: false,
        fileType: "",
        iframeUrl: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      unitList: [],
    };
  },
  created() {
    this.getList();
    //   this.getUnitList()
  },
  methods: {
    /** 查询外包单位资质管理列表 */
    getList() {
      this.loading = true;
      listemergencyDrill(this.queryParams).then((response) => {
        console.log(response);
        this.documentList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    showContent(data) {
      this.contenShow = true;
      this.contentList = data;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    upImage(e) {
      this.form.filePaths = e;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mineId: 1,
        drillName: null,
        drillUnit: null,
        drillPlace: null,
        drillTime: null,
        drillWay: null,
        organizationdep: null,
        actor: null,
        drillContent: null,
        filePaths: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.queryParams = {
      //   currentPage: 1,
      //   pageSize: 10,
      //   startTime: null,
      //   endTime: null,
      // };
      this.queryParams.drillName=null
      this.queryParams.startTime=null
      this.resetForm("queryForm");
      this.handleQuery();
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加年度计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //   const id = row.id || this.ids;

      this.form = { ...row };
      this.open = true;
      this.title = "修改年度计划";
      //   getDocument(id).then((response) => {
      //     this.form = response.data;
      //     this.open = true;
      //     this.title = "修改外包单位资质管理";
      //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            addemergencyDrill(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addemergencyDrill(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getCurrentTime() {
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, "0");
      const day = String(now.getDate()).padStart(2, "0");
      const hours = String(now.getHours()).padStart(2, "0");
      const minutes = String(now.getMinutes()).padStart(2, "0");
      const seconds = String(now.getSeconds()).padStart(2, "0");

      let currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      return currentTime;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除外包单位资质管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return deleteemergencyDrill({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "app/document/export",
        {
          ...this.queryParams,
        },
        `document_${new Date().getTime()}.xlsx`
      );
    },
    getUnitList() {
      listUnit({ pageNum: 1, pageSize: 100 }).then((res) => {
        // console.log(res);
        this.unitList = res.rows;
      });
    },
    download(value) {
      if (value.indexOf(",") >= 0) {
        let split = value.split(",");
        for (var i = 0; i < split.length; i++) {
          if (split[i] != "") {
            let num = split[i].lastIndexOf(".");
            let dis = split[i].substring(num + 1, num + 4);

            if (dis == "pdf" || dis == "PDF") {
              downloadFile(split[i], "a");
            } else {
              downloadFile(split[i], "iframe");
            }
          }
        }
      } else {
        let num = value.lastIndexOf(".");
        let dis = value.substring(num + 1, num + 4);
        if (dis == "pdf" || dis == "PDF") {
          downloadFile(value, "a");
        } else {
          downloadFile(value, "iframe");
        }
      }
    },
    gettp(v) {
      let type = null;
      if (v.includes(",")) {
        let list = v.split(",");
        list.forEach((item) => {
          type = this.getFileType(item);
        });
      } else {
        type = this.getFileType(v);
      }
      if (type == "pdf" || type == "txt") {
        return 1;
      } else {
        return 2;
      }
    },
    getFileType(url) {
      let pdfReg = /^.+(\.pdf)$/;
      let txtReg = /^.+(\.txt)$/;
      let wordReg = /^.+(\.doc|\.docx)$/;
      let excelReg = /^.+(\.xls|\.xlsx)$/;
      let jpgReg = /^.+(\.png|\.jpg|\.jpeg|\.bmp)$/;
      if (pdfReg.test(url)) {
        return "pdf";
      }
      if (txtReg.test(url)) {
        return "txt";
      }
      if (wordReg.test(url)) {
        return "word";
      }
      if (excelReg.test(url)) {
        return "excel";
      }
      if (jpgReg.test(url)) {
        return "picture";
      }
    },
    look(v) {
      this.showType = false;
      this.$nextTick(() => {
        this.showType = true;
      });
      let list = v.split(",");
      this.previewMsg.dialogTableVisible = true;
      this.previewMsg.iframeUrl = list[0];
    },
  },
};
</script>
    